<template>
	<view style="padding-bottom: 80upx;">
		<view class=" u-flex padding-lr bg padding-top">
			<view class="u-m-r-10">
				<image :src="avatar" style="width: 100rpx;height: 100rpx;border-radius: 100rpx;"
					@click="goNav('/my/store/index')"></image>
			</view>
			<view class="u-flex-1 u-m-l-10" v-if="!isLogin">
				<view class="u-font-18  text-bold">
					<view class="margin-left-sm margin-top-xs">
						<view class="flex align-end">{{userName}}
							<view class="margin-left-sm" v-if="shop" @click="changePutaWayFlag">
								<image
									:src="shop.putawayFlag==0?'../../static/images/index/shangban.png':'../../static/images/index/dayang.png'"
									style="width: 30rpx;height: 30rpx;"></image>
							</view>
							<!-- <view>
							<image src="../../static/images/index/dayang.png" style="width: 30rpx;height: 30rpx;"></image>
						</view> -->
						</view>
						<!-- 	<text @click="changePutaWayFlag"
							:style="shop.putawayFlag==0?'margin-left: 16upx;font-size: 28upx;color: #E10A07;':'margin-left: 16upx;font-size: 28upx;color: #999999;'"
							v-if="shop">{{shop.putawayFlag==0?'上班中':'打烊啦'}}

						</text> -->
					</view>
				</view>

			</view>

			<view v-else class="text-xl u-p-l-20 text-bold" @click="goLogin('/pages/my/loginphone')">
				登录
			</view>
		</view>

		<view class="flex align-center padding-left">
			<image src="../../static/images/img/data.png" style="width: 26upx;height: 26upx;"></image>
			<view class="margin-left-xs flex align-center" style="color: #999999;">
				<view @click="bindData(1)">{{startTime?startTime:'开始时间'}}</view>
				至
				<view @click="bindData(2)">{{endTime?endTime:'结束时间'}}</view>
			</view>
		</view>
		<view class="flex align-center box justify-between text-center" style="padding: 0 80rpx;">
			<view @click="goNav('/my/other/cashList')">
				￥<text class="text-bold text-xl">{{allMoney?allMoney:'0'}}</text>
				<view>总收益</view>
			</view>
			<!-- <view class="">
				<text class="text-bold text-xl">{{allCount?allCount:'0'}}</text>
				<view>总销量</view>
			</view> -->
			<view @click="goNav('/my/store/pingjia')">
				<text class="text-bold text-xl">{{countTakingByUserId?countTakingByUserId:'0'}}</text>
				<view>用户评价</view>
			</view>
			<!-- 	<view class="">
				<text class="text-bold text-xl">{{putaway+soldOut}}</text>
				<view>用户发布</view>
			</view> -->
		</view>
		<view class="padding">
			<view class="text-bold text-lg margin-bottom-sm">订单数据</view>
			<view class="orderbox ">
				<view class="">
					￥<text class="text-bold text-xl">{{orderMoney?orderMoney:'0'}}</text>
					<view class="margin-top-xs">订单收入</view>
				</view>
				<view class="">
					<text class="text-bold text-xl">{{sumCountOrder?sumCountOrder:'0'}}</text>
					<view class="margin-top-xs">总订单数</view>
				</view>
				<view class="">
					￥<text class="text-bold text-xl">{{refundMoney?refundMoney:'0'}}</text>
					<view class="margin-top-xs">退款金额</view>
				</view>
				<!-- <view class="">
					<text class="text-bold text-xl">{{countByUserId?countByUserId:'0'}}</text>
					<view class="margin-top-xs">访客人数</view>
				</view> -->
			</view>
		</view>
		<view style="padding: 0upx 30upx;">
			<view class="text-bold text-lg margin-bottom-sm flex justify-between">
				<view>订单管理</view>
				<view @click="goDetail(1)">
					<text style="color: #666666;font-size: 26upx;margin-right: 10upx;">查看全部</text>
					<image src="../../static/images/my/right.png" style="width: 12upx;height: 20upx;"></image>
				</view>
			</view>
			<view class="orderbox ">
				<!-- <view class="">
					<text class="text-bold text-xl">{{i1}}</text>
					<view class="margin-top-xs">待付款</view>
				</view> -->
				<view class="">
					<text class="text-bold text-xl">{{i5}}</text>
					<view class="margin-top-xs">待服务</view>
				</view>
				<view class="">
					<text class="text-bold text-xl">{{i2}}</text>
					<view class="margin-top-xs">进行中</view>
				</view>
				<view class="">
					<text class="text-bold text-xl">{{i3}}</text>
					<view class="margin-top-xs">已完成</view>
				</view>
				<view class="">
					<text class="text-bold text-xl">{{i4}}</text>
					<view class="margin-top-xs">待接单</view>
				</view>
			</view>
		</view>
		<view class="padding">
			<view class="text-bold text-lg margin-bottom-sm flex justify-between">
				<view>我的服务</view>
				<view @click="goDetail(2)">
					<text style="color: #666666;font-size: 26upx;margin-right: 10upx;">查看全部</text>
					<image src="../../static/images/my/right.png" style="width: 12upx;height: 20upx;"></image>
				</view>
			</view>
			<view class="orderbox ">
				<view class="">
					<text class="text-bold text-xl">{{putawayCount}}</text>
					<view class="margin-top-sm">开启服务</view>
				</view>
				<view class="">
					<text class="text-bold text-xl">{{soldoutCount}}</text>
					<view class="margin-top-sm">禁用服务</view>
				</view>
				<view @click="bindFb(1)">
					<image src="../../static/images/index/add.png" style="width: 46upx;height: 46upx;"></image>
					<view class="margin-top-xs">添加服务</view>
				</view>

			</view>
		</view>
		<view class="padding">
			<view class="text-bold text-lg margin-bottom-sm flex justify-between">
				<view>服务医院</view>
				<view @click="goDetail(3)">
					<text style="color: #666666;font-size: 26upx;margin-right: 10upx;">查看全部</text>
					<image src="../../static/images/my/right.png" style="width: 12upx;height: 20upx;"></image>
				</view>
			</view>
			<view class="orderbox ">
				<view class="">
					<text class="text-bold text-xl">{{serviceHospitalList.length}}</text>
					<view class="margin-top-sm">服务过的医院数量</view>
				</view>
			</view>
		</view>
		<view class="padding">
			<view class="text-bold text-lg margin-bottom-sm flex justify-between">
				<view>我的陪诊/陪护师</view>
				<view @click="goDetail(4)">
					<text style="color: #666666;font-size: 26upx;margin-right: 10upx;">查看全部</text>
					<image src="../../static/images/my/right.png" style="width: 12upx;height: 20upx;"></image>
				</view>
			</view>
			<view class="orderbox ">
				<view class="">
					<text class="text-bold text-xl">{{onlineShiFuCount}}</text>
					<view class="margin-top-sm">上线人数</view>
				</view>
				<view class="">
					<text class="text-bold text-xl">{{downLineShiFuCount}}</text>
					<view class="margin-top-sm">下线人数</view>
				</view>
				<view @click="bindFb(2)">
					<image src="../../static/images/index/add.png" style="width: 46upx;height: 46upx;"></image>
					<view class="margin-top-xs">绑定陪诊/陪护师</view>
				</view>

			</view>
		</view>
		<u-popup v-model="Authorization" mode="center" close-icon="close-circle" close-icon-pos="top-right"
			close-icon-color="#8f9298" close-icon-size="50">
			<view class="contentview">
				<view class="title">隐私保护指引</view>
				<view class="des" @click="openPrivacyContract">
					在使用当前小程序服务之前，请仔细阅读<text
						style="color: #FF7F00;">{{privacyContractName}}</text>。如你同意{{privacyContractName}}，请点击“同意”开始使用。
				</view>
				<view class="btns">
					<button class="item reject" @click="exitMiniProgram">拒绝</button>
					<button id="agree-btn" class="item agree" open-type="agreePrivacyAuthorization"
						@agreeprivacyauthorization="handleAgreePrivacyAuthorization">同意</button>
				</view>
			</view>
		</u-popup>
		<!-- 开始时间 -->
		<u-picker v-model="startshow" mode="time" :params="paramsStart" @confirm="startData"></u-picker>
		<!-- 结束时间 -->
		<u-picker v-model="endshow" mode="time" :params="paramsEnd" @confirm="endData"></u-picker>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				Authorization: false,
				privacyContractName: '',
				avatar: '../../static/logo.png',
				isLogin: true,
				userName: '匿名',

				startshow: false,
				endshow: false,
				paramsStart: {
					year: true,
					month: true,
					day: true,
					hour: false,
					minute: false,
					second: false
				},
				paramsEnd: {
					year: true,
					month: true,
					day: true,
					hour: false,
					minute: false,
					second: false
				},
				startTime: '',
				endTime: '',

				allMoney: 0, //总收益
				allCount: 0, //总销量
				countTakingByUserId: 0, //用户评价数
				orderMoney: 0, //订单收入
				sumCountOrder: 0, //总订单数
				refundMoney: 0, //退款金额
				countByUserId: 0, //访客人数
				i1: 0, //待付款
				i2: 0, //制作中
				i3: 0, //待取餐
				i4: 0, //已完成
				i5: 0, //商家待接单
				putawayCount: 0, //开启服务
				soldoutCount: 0, //禁用服务
				page: 1,
				limit: 10,
				longitude: '',
				latitude: '',
				userId: '',
				shop: '',
				arr: [],
				errCount: 0,
				serviceHospitalList: [],
				downLineShiFuCount: [],
				onlineShiFuCount: [],

			}
		},
		onLoad() {
			this.userId = uni.getStorageSync('userId')

			if (this.userId) {
				this.getFb()
			}


			//转账通知
			this.$Request.getT('/app/common/type/322').then(res => { //商户转账通知 322
				if (res.code == 0) {
					if (res.data && res.data.value) {
						this.arr.push(res.data.value)
					}
				}
			})

			//撤销转账通知
			this.$Request.getT('/app/common/type/351').then(res => { //商户撤销转账通知 351
				if (res.code == 0) {
					if (res.data && res.data.value) {
						this.arr.push(res.data.value)
					}
				}
			})



			// #ifdef MP-WEIXIN
			try {
				let that = this;
				wx.getPrivacySetting({
					success: res => {
						console.log("是否需要授权：", res.needAuthorization, "隐私协议的名称为：", res.privacyContractName)
						if (res.needAuthorization) {
							that.privacyContractName = res.privacyContractName;
							// this.$refs.popusAuthorization.open();
							that.Authorization = true;
						}
					},
					fail: () => {},
					complete: () => {},
				})
			} catch (e) {
				//TODO handle the exception
			}
			// #endif
		},
		onShow() {
			this.userId = uni.getStorageSync('userId')
			if (this.userId) {
				this.isLogin = false
				this.getUserInfo()
				this.getFb()
				// this.getTodayOrder()
				this.getcity()
				this.getShopInfo();
				// #ifdef MP-WEIXIN
				//订阅
				if (!uni.getStorageSync('sendindexMsg')) {
					this.openMsg();
				}
				// #endif
			} else {
				this.isLogin = true
				// this.userName = '匿名'
				this.avatar = '../../static/logo.png'
				this.allMoney = 0 //总收益
				this.putawayCount = 0 //上架中
				this.soldoutCount = 0 //下架中
				this.allCount = 0 //总销量
				this.countTakingByUserId = 0 //用户评价
				this.orderMoney = 0 //订单收入
				this.sumCountOrder = 0 //总订单数
				this.refundMoney = 0 //退款金额
				this.countByUserId = 0 //访客人数
				this.i1 = 0 //待付款
				this.i3 = 0 //待取餐
				this.i2 = 0 //制作中
				this.i4 = 0 //已完成
				this.i5 = 0 //商家待接单
			}
			let that = this
			if (that.userId) {
				setInterval(function() {
					// 订单消息提醒
					// newsSelect() {
					let data = {
						shopIds: that.$queue.getData("shopId")
					}
					that.$Request.post('/admin/ordermessage/selectReadFlagCount', data).then(res => {
						if (res.code === 0) {
							// that.errCount = res.data
							if (res.data > 0) {
								if (that.errCount != res.data) {
									that.errCount = res.data
									that.aplayAudio()
									uni.showModal({
										title: '消息',
										content: '有' + res.data + '条订单，请前往订单中心处理。',
										success: (ret) => {
											if (ret.confirm) {
												uni.switchTab({
													url: '/pages/order/index'
												})

											} else {
												console.log('else', ret)
												that.defineCallBack()
											}
										}
									});
									// this.chatNum = data.data
								}
							} else {
								that.errCount = 0
							}
						}
					});
				}, 10000 * 100) //时间修改++++++++++++++++++++原10000
			}


		},
		methods: {
			// 打开隐私协议页面
			openPrivacyContract() {
				let that = this;
				wx.openPrivacyContract({
					fail: () => {
						that.$queue.showToast('遇到错误无法打开！');
					}
				})
			},
			// 拒绝隐私协议
			exitMiniProgram() {
				// 直接退出小程序
				wx.exitMiniProgram()
			},
			// 同意隐私协议
			handleAgreePrivacyAuthorization() {
				this.Authorization = false;
			},
			openMsg() {
				var that = this
				wx.getSetting({
					withSubscriptions: true, //是否获取用户订阅消息的订阅状态，默认false不返回
					success(ret) {
						if (ret.subscriptionsSetting.itemSettings) {
							uni.setStorageSync('sendindexMsg', true)
							uni.openSetting({ // 打开设置页 
								success(rea) {
									console.log(rea.authSetting)
								}
							});
						} else { // 用户没有点击“总是保持以上，不再询问”则每次都会调起订阅消息
							uni.setStorageSync('sendindexMsg', false)
							uni.showModal({
								title: '提示',
								content: '为了更好的体验,请绑定消息推送',
								confirmText: '确定',
								cancelText: '取消',
								success: function(res) {
									if (res.confirm) {
										uni.requestSubscribeMessage({
											tmplIds: that.arr,
											success(re) {
												// console.log(re,'**********')
												var datas = JSON.stringify(re);
												if (datas.indexOf("accept") != -1) {
													console.log(re)
												}
											},
											fail: (res) => {
												console.log(res)
											}
										})
										that.showModal = false
									} else if (res.cancel) {
										that.showModal = true
									}
								}
							})
						}
					}
				})
			},
			//切换店铺状态
			changePutaWayFlag() {

				if (this.shop.putawayFlag == 0) {
					uni.showModal({
						title: '提示',
						content: '确定要打烊了吗？',
						showCancel: true,
						cancelText: '取消',
						confirmText: '确定',
						success: res => {
							if (res.confirm) {
								this.shop.putawayFlag = 1
								this.updateShopInfo();
							}
						},
						fail: () => {},
						complete: () => {}
					});

				} else {

					this.shop.putawayFlag = 0
					this.updateShopInfo();
				}


			},
			updateShopInfo() {
				this.$Request.postJsonA("/app/shop/updateShopMessage", this.shop).then(res => {
					if (res.code == 0) {
						this.getShopInfo()
					} else {
						this.shop.putawayFlag = 1
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				});
			},
			getShopInfo() {
				if (this.$queue.getData('shopId')) {
					let data = {
						shopId: this.$queue.getData('shopId')
					}
					this.$Request.getA("/admin/shop/selectShopMessage", data).then(res => {
						if (res.code == 0) {
							uni.hideLoading()

							this.shop = res.data
							this.$queue.setData("city", res.data.city);
						}
					});
				}

			},
			aplayAudio() {
				// const audio = document.getElementById('audio')

				// audio.play()
				// console.log('语音提示')
				const innerAudioContext = uni.createInnerAudioContext();
				innerAudioContext.autoplay = true;
				// innerAudioContext.src ='../../static/mpc/order.mp3';
				innerAudioContext.src =
					'https://pw.xianmxkj.com/file/uploadPath/2022/01/19/0753211f78d718d44ee6372e33eae9ee.mp3';
				innerAudioContext.onPlay(() => {
					console.log('开始播放');
				});
				innerAudioContext.onError((res) => {
					console.log(res.errMsg);
					console.log(res.errCode);
				});
			},
			defineCallBack() {
				let that = this
				let data = {
					shopIds: that.$queue.getData("shopId")
				}
				that.$Request.post('/admin/ordermessage/allcheckOrderMessage', data).then(res => {
					if (res.code === 0) {
						that.errCount = 0

					}
				});
			},
			getcity() {
				let that = this
				uni.getLocation({
					type: 'wgs84',
					success: function(res) {
						// console.log('当前位置的经度：' + res.longitude);
						// console.log('当前位置的纬度：' + res.latitude);
						that.latitude = res.latitude
						that.longitude = res.longitude
					}
				});
			},
			//获取用户发布
			getFb() {
				let data = {
					startTime: this.startTime,
					endTime: this.endTime,
					shopId: this.$queue.getData("shopId")
				}
				this.$Request.getA("/app/shop/shopHomePage", data).then(res => {
					if (res.code == 0) {
						console.log(res, 'res');
						this.allMoney = res.data.allMoney //总收益
						this.putawayCount = res.data.enableServiceCount //上架中
						this.soldoutCount = res.data.disableServiceCount //下架中
						this.allCount = res.data.allCount //总销量
						this.countTakingByUserId = res.data.countTakingByUserId //用户评价
						this.serviceHospitalList = res.data.serviceHospitalList //医院列表
						this.onlineShiFuCount = res.data.onlineShiFuCount //上线人数
						this.downLineShiFuCount = res.data.downLineShiFuCount //下线人数




						this.orderMoney = res.data.orderMoney //订单收入
						this.sumCountOrder = res.data.sumCountOrder //总订单数
						this.refundMoney = res.data.refundMoney //退款金额
						this.countByUserId = res.data.countByUserId //访客人数
						this.i1 = res.data.i1 //待付款
						this.i3 = res.data.i3 //进行中
						this.i2 = res.data.i2 //已完成
						this.i4 = res.data.i4 //已取消
						this.i5 = res.data.i5 //待服务
					}
				})
			},

			goDetail(index) {
				let userId = uni.getStorageSync('userId')
				if (userId) {
					// // #ifdef MP-WEIXIN
					// if (uni.getStorageSync('sendindexMsg')) {
					// 	uni.requestSubscribeMessage({
					// 		tmplIds: this.arr,
					// 		success(re) {
					// 			// console.log(re,'**********')
					// 			var datas = JSON.stringify(re);
					// 			if (datas.indexOf("accept") != -1) {
					// 				console.log(re)
					// 			}
					// 		},
					// 		fail: (res) => {
					// 			console.log(res)
					// 		}
					// 	})
					// }
					// // #endif
					if (index == 1) {
						uni.switchTab({
							url: '/pages/order/index'
						})
					} else if (index == 2) {
						uni.navigateTo({
							url: '/my/store/addgood'
						})
					} else if (index == 3) {

						if (this.serviceHospitalList.length > 0) {
							uni.navigateTo({
								url: '/my/hospital/index'
							})
						} else {
							uni.showModal({
								title: '提示',
								content: '医院列表为空',
							})
						}

					} else if (index == 4) {
						uni.navigateTo({
							url: '/my/diagnosis/index'
						})
					}
				} else {
					uni.showModal({
						title: '提示',
						content: '您还未登录,请先登录',
						success: function(res) {
							if (res.confirm) {
								// console.log('用户点击确定');
								uni.navigateTo({
									url: '/pages/my/loginphone'
								})
							} else if (res.cancel) {
								// console.log('用户点击取消');
							}
						}
					})
				}

			},
			bindFb(e) {
				let userId = uni.getStorageSync('userId')
				if (userId) {
					
					if(e == 1) {
						
						uni.navigateTo({
							url: '/my/publish/goods'
						})
					}else if(e == 2) {
						uni.navigateTo({
							url: '/my/diagnosis/addUserList'
						})
					}
					
					

				} else {
					uni.showModal({
						title: '提示',
						content: '您还未登录,请先登录',
						success: function(res) {
							if (res.confirm) {
								// console.log('用户点击确定');
								uni.navigateTo({
									url: '/pages/my/loginphone'
								})
							} else if (res.cancel) {
								// console.log('用户点击取消');
							}
						}
					})
				}
			},
			//时间弹框开关
			bindData(index) {
				if (index == 1) {
					this.startshow = true
				} else if (index == 2) {
					this.endshow = true

				}
			},
			//开始时间
			startData(e) {

				this.startTime = e.year + '-' + e.month + '-' + e.day
			},
			// 结束时间
			endData(e) {
				this.endTime = e.year + '-' + e.month + '-' + e.day

				this.getFb()
			},
			goNav(e, name) {
				if (this.userId) {
					// #ifdef MP-WEIXIN
					if (uni.getStorageSync('sendindexMsg')) {
						uni.requestSubscribeMessage({
							tmplIds: this.arr,
							success(re) {
								// console.log(re,'**********')
								var datas = JSON.stringify(re);
								if (datas.indexOf("accept") != -1) {
									console.log(re)
								}
							},
							fail: (res) => {
								console.log(res)
							}
						})
					}
					// #endif
					uni.navigateTo({
						url: e
					})
				} else {
					uni.showModal({
						title: '提示',
						content: '您还未登录,请先登录',
						success: function(res) {
							if (res.confirm) {
								console.log('用户点击确定');
								uni.navigateTo({
									url: '/pages/my/loginphone'
								})
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					})
				}
			},
			getUserInfo() {
				this.$Request.getA("/sys/user/info").then(res => {
					if (res.code == 0) {

						
						this.avatar = res.user.userEntity.avatar ? res.user.userEntity.avatar :
							'../../static/logo.png'
						// this.userName = res.user.shopList[0].shopName
						// this.avatar = res.user.shopList[0].shopCover ? res.user.shopList[0].shopCover :
						'../../static/logo.png'
						this.$queue.setData("userId", res.user.userId);
						this.$queue.setData("shopId", res.user.shopList[0].shopId);
						this.$queue.setData("mobile", res.user.mobile);
						
						
						if (res.user.userEntity) {
							this.userName = res.user.userEntity.userName
							this.$queue.setData("shopUserName", res.user.userEntity.userName);
							this.$queue.setData("myUserId", res.user.userEntity.userId);
						}
					}
				});
			},
			goLogin(e) {
				uni.navigateTo({
					url: e
				})
			},

		}
	}
</script>

<style lang="scss">
	page {
		background-color: #FFFFFF;
	}

	.bg {
		background-color: #FFFFFF;
	}

	.box {
		width: 690upx;
		margin: 0 auto;
		height: 120rpx;
		background: #FCD202;
		border-radius: 16upx 16upx 0px 0px;
		color: #333333;
		padding: 0upx 30upx;
		margin-top: 20upx;
	}

	.orderbox {
		width: 690upx;
		margin: 0 auto;
		height: 153upx;
		background: #FAFAFA;
		border-radius: 16upx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		text-align: center;
		padding: 0upx 30upx;
	}

	.privacy {
		position: fixed;
		top: 0;
		right: 0;
		bottom: 0;
		left: 0;
		background: rgba(0, 0, 0, .5);
		z-index: 9999999;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.contentview {
		width: 632rpx;
		padding: 48rpx;
		box-sizing: border-box;
		background: #fff;
		border-radius: 16rpx;
	}

	.contentview .title {
		text-align: center;
		color: #333;
		font-weight: bold;
		font-size: 32rpx;
	}

	.contentview .des {
		font-size: 26rpx;
		color: #666;
		margin-top: 40rpx;
		text-align: justify;
		line-height: 1.6;
	}

	.contentview .des .link {
		color: #07c160;
		text-decoration: underline;
	}

	button::after {
		border: none;
	}

	.btns {
		margin-top: 48rpx;
		display: flex;
	}

	.btns .item {
		justify-content: space-between;
		width: 244rpx;
		height: 80rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		border-radius: 16rpx;
		box-sizing: border-box;
		border: none;
	}

	.btns .reject {
		background: #f4f4f5;
		color: #909399;
	}

	.btns .agree {
		background: #07c160;
		color: #fff;
	}
</style>